package com.es.disped.web.controller.lesson;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.lesson.LessonService;
import com.es.disped.common.constant.RecordStatus;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.Lesson;
import com.es.disped.dao.model.LessonExample;
import com.es.disped.dao.model.SyllabusForeignModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月5日 下午4:41:01<br>
 *  名称：LessonController.java<br>
 *  描述：课时管理<br>
 */
@Api(value="课时管理")
@Controller
@RequestMapping("/manage/lesson")
public class LessonController extends BaseController{

	private final String SYLLABUS_NAMESPACE="com.es.disped.dao.mapper.syllabus.SyllabusCustomMapper";
	private final String LESSON_NAMESPACE="com.es.disped.dao.mapper.lesson.LessonCustomMapper";
	
	@Autowired
	LessonService lessonService;
	
	@ApiOperation(value="新增课时",response=MsgModel.class,httpMethod="POST",notes="新增课时对象")
	@RequiresPermissions(value={"admin:save","sys:save"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="lesson",value="课时对象",required=true) Lesson lesson)
	{
		int count=0;
		if(Optional.of(lesson)
				.filter(lname->lesson.getLessonName()!=null)
				.filter(syId->lesson.getSyllabusId()!=null)
				.filter(content->lesson.getLessonContent()!=null)
				.filter(btime->lesson.getLessonBeginTime()!=null)
				.filter(etime->lesson.getLessonEndTime()!=null)
				.isPresent())
		{
			lesson.setLessonId(getUUID());
			SyllabusForeignModel syllabusForeignModel=this.getService().selectOne(
					SYLLABUS_NAMESPACE+".getSyllabusForeignModel", lesson.getSyllabusId());
			lesson.setSyllabusName(syllabusForeignModel.getSyllabusName());
			
			LessonExample countExample=new LessonExample();
			LessonExample.Criteria criteria=countExample.createCriteria();
			criteria.andSyllabusIdEqualTo(lesson.getSyllabusId()).andLessonValidEqualTo(RecordStatus.USEAGE.getValue());
			int total=lessonService.countByExample(countExample);
			lesson.setLessonIndex(total+1);
			
			lesson.setLessonValid(RecordStatus.USEAGE.getValue());
			count=lessonService.insertSelective(lesson);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"保存成功":"保存失败",count==1?lesson:"");
	}
	
	
	@ApiOperation(value="更新课时",response=MsgModel.class,httpMethod="POST",notes="更新课时对象")
	@RequiresPermissions(value={"admin:update","sys:update"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="lesson",value="课时对象",required=true) Lesson lesson)
	{
		int count=0;
		if(!this.isNull(lesson.getLessonId()))
		{
			if(!this.isNull(lesson.getSyllabusId()))
			{
				SyllabusForeignModel syllabusForeignModel=this.getService().selectOne(
						SYLLABUS_NAMESPACE+".getSyllabusForeignModel", lesson.getSyllabusId());
				lesson.setSyllabusName(syllabusForeignModel.getSyllabusName());
			}
			count=lessonService.updateByPrimaryKey(lesson);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"更新成功":"更新失败",count==1?lesson:"");
	}
	
	
	@ApiOperation(value="删除课时",response=MsgModel.class,httpMethod="POST",notes="删除课时对象")
	@RequiresPermissions(value={"admin:delete","sys:delete"},logical=Logical.OR)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="lesson",value="课时对象",required=true) Lesson lesson)
	{
		int count=0;
		if(!this.isNull(lesson.getLessonId()))
		{
			count=this.getService().delete(LESSON_NAMESPACE+".deleteLessonModel", lesson.getLessonId());
		}
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"删除成功":"删除失败",count>=1?lesson:"");
	}
	
	@ApiOperation(value="检索课时",response=MsgModel.class,httpMethod="POST",
			notes="检索课时对象，精确检索：课时ID，教学计划ID；模糊检索：教学大纲名称，课时名称，课时开始时间+结束时间")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="lesson",value="课时对象",required=false) Lesson lesson)
	{
		LessonExample listExample=new LessonExample();
		LessonExample.Criteria criteria=listExample.createCriteria();
		if(!this.isNull(lesson.getLessonId()))
		{
			criteria.andLessonIdEqualTo(lesson.getLessonId());
		}
		if(!this.isNull(lesson.getSyllabusId()))
		{
			criteria.andSyllabusIdEqualTo(lesson.getSyllabusId());
		}
		if(!this.isNull(lesson.getSyllabusName()))
		{
			criteria.andSyllabusNameLike("%"+lesson.getSyllabusName()+"%");
		}
		if(!this.isNull(lesson.getLessonName()))
		{
			criteria.andLessonNameLike("%"+lesson.getLessonName()+"%");
		}
		if(!this.isNull(lesson.getLessonBeginTime()))
		{
			criteria.andLessonBeginTimeGreaterThanOrEqualTo(lesson.getLessonBeginTime());
		}
		if(!this.isNull(lesson.getLessonEndTime()))
		{
			criteria.andLessonEndTimeLessThanOrEqualTo(lesson.getLessonEndTime());
		}
		List<Lesson> list=lessonService.selectByExample(listExample);
		return this.resultMsg(!this.isNull(list)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(list)?"课时列表加载成功":"课时列表加载失败",!this.isNull(list)?list:"");
	}
	
	
	@ApiOperation(value="分页加载课时",response=MsgModel.class,httpMethod="POST",notes="分页加载课时，分页参数['offset','limit']")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		LessonExample listExample=new LessonExample();
		PageModel<Lesson> pageModel=null;
		if(Optional.of(paramMap)
				.filter(offset->paramMap.containsKey("offset")!=false)
				.filter(limit->paramMap.containsKey("limit")!=false)
				.isPresent())
		{
			pageModel=lessonService.selectByExampleForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
			
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"课时加载成功":"课时加载失败",!this.isNull(pageModel)?pageModel:"");
	}
}
